//
//  ViewController.m
//  superman
//
//  Created by Martin Hadzhiyski on 2/24/14.
//  Copyright (c) 2014 Martin Hadzhiyski. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property UIImageView* supermanView;
@property UIButton* flyButton;
@property int clicksCounter;
@property NSTimer* timer;
@property UIButton* superManButton;
@property BOOL isMovingVertical;
@end

@implementation ViewController


- (void)viewDidLoad
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(flyAction) userInfo:nil repeats:YES];
    
    
    [super viewDidLoad];
    self.supermanView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"superman.jpg"]];
    self.supermanView.frame = CGRectMake(self.view.center.x, 320, 70, 130);
    self.supermanView.center = CGPointMake(self.view.center.x, self.supermanView.center.y);
    [self.view addSubview:self.supermanView];
    
    [self.view addSubview:self.flyButton];
    self.superManButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview: self.superManButton];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) flyAction
{
    static BOOL isHeGoingUp = YES;
    self.isMovingVertical = YES;
    
    if (self.supermanView.frame.origin.x + self.supermanView.frame.size.width < self.view.bounds.size.width) {
        
        if (self.supermanView.frame.origin.y >= 20 && isHeGoingUp) {
            self.supermanView.frame = CGRectMake(self.supermanView.frame.origin.x, self.supermanView.frame.origin.y - 10, self.supermanView.frame.size.width, self.supermanView.frame.size.height);
            if (self.supermanView.frame.origin.y == 20) {
                isHeGoingUp = NO;
                self.supermanView.transform = CGAffineTransformMakeRotation(M_PI);
            }
        }
        else if (self.supermanView.frame.origin.y <= 350) {
            self.supermanView.frame = CGRectMake (self.supermanView.frame.origin.x, self.supermanView.frame.origin.y + 10, self.supermanView.frame.size.width, self.supermanView.frame.size.height);
            if (self.supermanView.frame.origin.y == 350) {
                isHeGoingUp = YES;
                self.supermanView.transform = CGAffineTransformRotate(CGAffineTransformMakeRotation(M_PI), M_PI);
            }
            
        }
    }
    
    
    self.superManButton.frame = CGRectMake(self.supermanView.frame.origin.x, self.supermanView.frame.origin.y, 100, 150);
    [self.superManButton setTitle: @"" forState: UIControlStateNormal];
    
    [self.superManButton addTarget:nil action:@selector(supermanButtonAction) forControlEvents: UIControlEventTouchUpInside];
    
}

- (void) supermanButtonAction {
    self.isMovingVertical = NO; 
    
}

@end
